
<nav class="panel">
  {{ $currentPage := . }}
  {{ range .Site.Menus.main }}
    {{ $expandChildren := (not .Page.Params.collapseChildren) }}
    <a href="{{.URL}}" class="panel-block {{ if eq $currentPage .Page }}is-active{{ end }}">
      {{if .Page.Params.iconClass}}
      <span class="panel-icon">
        <i class="{{.Page.Params.iconClass}}" aria-hidden="true"></i>
      </span>
      {{end}}
      {{ .Name }}
    </a>

    {{ range .Page.Pages.ByWeight }}
      {{ $currentFilePage := (eq $currentPage .)}}
      {{ $siblingOfCurrentFilePage := (eq $currentPage.Parent .Parent)}}
      {{ $childOfCurrentDirPage := (eq $currentPage .Parent)}}
      {{ if or $currentFilePage (and $siblingOfCurrentFilePage $expandChildren) (and $childOfCurrentDirPage $expandChildren) }}
        {{ $pageUrl := (cond (isset .Params "external_url") .Params.external_url .URL) }}
        <a href="{{$pageUrl}}" style="padding-left: 1.5em" class="panel-block {{ if eq $currentPage . }}is-active{{ end }}">
          <span class="panel-icon">
            <i class="fas fa-book" aria-hidden="true"></i>
          </span>
          {{ .Name }}
        </a>

        {{ if eq $currentPage . }}
          {{ $headings := findRE "<h1.*?>(.|\n])+?</h1>" .Content }}
          {{ range $headings }}
            <a href="#{{ . | replaceRE "&rsquo;" " " | replaceRE "\\." " " | htmlUnescape | plainify | urlize  }}" style="padding-left: 3em" class="panel-block">
              <span class="panel-icon">
                <i class="fas fa-angle-right" aria-hidden="true"></i>
              </span>
              {{ . | htmlUnescape | plainify }}
            </a>
          {{ end }}
        {{ end }}
      {{ end }}
    {{ end }}
  {{ end }}
</nav>
